Read the instructions on Canvas carefully!
Name: Gianni Spiga
Student ID: 918363295
library(lme4)
library(ggplot2)
library(plotly)
In this exam, we investigate the ChickWeight dataset in
R. You can load the data using the following commands.
Carefully read the help file of ChickWeight before working
on the following questions.
data(ChickWeight)
View(ChickWeight)
(a) Briefly summarize all variables in the data set. You need to provide the definition of the variable and quantitative summary.
Solution: (Type your answer here)
# (a). (Type your code in the space below, if any)
summary(ChickWeight)
## weight Time Chick Diet
## Min. : 35.0 Min. : 0.00 13 : 12 1:220
## 1st Qu.: 63.0 1st Qu.: 4.00 9 : 12 2:120
## Median :103.0 Median :10.00 20 : 12 3:120
## Mean :121.8 Mean :10.72 10 : 12 4:118
## 3rd Qu.:163.8 3rd Qu.:16.00 17 : 12
## Max. :373.0 Max. :21.00 19 : 12
## (Other):506
attributes(ChickWeight)
## $names
## [1] "weight" "Time" "Chick" "Diet"
##
## $row.names
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## [145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## [163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## [181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## [199] 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## [217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## [235] 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## [253] 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## [271] 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## [289] 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## [307] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## [325] 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## [343] 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## [361] 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## [379] 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## [397] 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## [415] 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## [433] 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
## [451] 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
## [469] 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
## [487] 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
## [505] 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
## [523] 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
## [541] 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
## [559] 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
## [577] 577 578
##
## $class
## [1] "nfnGroupedData" "nfGroupedData" "groupedData" "data.frame"
##
## $formula
## weight ~ Time | Chick
## <environment: R_EmptyEnv>
##
## $outer
## ~Diet
## <environment: R_EmptyEnv>
##
## $labels
## $labels$x
## [1] "Time"
##
## $labels$y
## [1] "Body weight"
##
##
## $units
## $units$x
## [1] "(days)"
##
## $units$y
## [1] "(gm)"
?ChickWeight # Attach definitions
## starting httpd help server ... done
table(ChickWeight$Chick)
##
## 18 16 15 13 9 20 10 8 17 19 4 6 11 3 1 12 2 5 14 7 24 30 22 23 27 28
## 2 7 8 12 12 12 12 11 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12
## 26 25 29 21 33 37 36 31 39 38 32 40 34 35 44 45 43 41 47 49 46 50 42 48
## 12 12 12 12 12 12 12 12 12 12 12 12 12 12 10 12 12 12 12 12 12 12 12 12
From the summary output above, we can see that the we have two quantitative variables, weight and Time. Our other two variables, chick and Diet are qualitative. We can see that for Chick, we have 12 counts for each chick, except for a few, specifically 18, 16, 15, 13, 8, and 35. From our Diet variable, we can see that there are more chicks on diet 1 than any other. Our time variable, given that the mean and median are close to each other, seems somewhat symmetric, however visualziation will be necessary. In our weight variable, we can see the mean is higher than the median, so we might expect some skew, which can be explored again in the visualization.
(b) Visualize the weights of each chicks over time in one
plot, where (i) each chick is represented by one solid curve, and (ii)
the diet is color-coded as black (1), red (2), green (3),
and blue (4). In addition to the required visualization, you may add any
supporting curves, symbols, or any additional plots that you find
informative.
Solution: (Type your answer here)
# (b). (Type your code in the space below, if any)
ggplotly(ggplot(data = ChickWeight, aes(
x = Time,
y = weight,
group = Chick,
color = Diet
)) + geom_line() + scale_color_manual(values = c("black", "red", "green", "blue")))
(c) Write down an appropriate one-way ANOVA model to answer the question whether there is any changes in mean weights at Day 20 across the four diet group. To receive full credits, you need to (i) write down the model, explain your notation, constraint(s) and/or assumptions; (ii) state the null and alternative hypotheses; (iii) state the test result. You can find basic LaTeX commands at the end of this file.
Solution: (Type your answer here)
\[ Y_{i} = \mu + \alpha_i + \epsilon_i \]
ii.) \[ H_0: \alpha_1 = \alpha_2 = \cdots = \alpha_4=0 \ \\ {\rm v.s.} \ \ H_1: {\rm not \ all\ } \alpha_i\ {\rm are\ the\ zero}. \]
# (c). (Type your code in the space below, if any)
chick.day20 <- ChickWeight[ChickWeight$Time == 20,]
mod1 <- aov(weight ~ Diet, data = chick.day20)
summary(mod1)
## Df Sum Sq Mean Sq F value Pr(>F)
## Diet 3 55881 18627 5.464 0.00291 **
## Residuals 42 143190 3409
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
From our ANOVA p-value of 0.003, we can reject the null hypothesis and conclude that there is one significant change in weight from the diet given to a chick.
(d) For the model fitted in (c), carry out necessary diagnostics to check if the model assumptions are valid. What are your findings?
Solution: (Type your answer here)
# (d). (Type your code in the space below, if any)
# Checking normality and equal var
plot(mod1, which = c(2,5))
Model looks good by normality and equal variance assumption
(e) Write down an appropriate two-way ANOVA model with fixed
effect to answer the question whether there is any differences in growth
rates across the four diet groups. Here the growth rate can be roughly
seen as the effects of Time on weight. To
receive full credits, you need to (i) write down the model, explain your
notation, constraint(s) and/or assumptions; (ii) state the null and
alternative hypotheses; (iii) state the test result. Hint: You may want
to recycle the answer in (c) to save time.
Solution: (Type your answer here)
\[ Y_{i,j,k}=\mu + \alpha_i+\beta_j +(\alpha \beta)_{i,j} +\epsilon_{i,j,k}, \\ i=1,\ldots, 12, j =1,\ldots, 4, k=1,2,\ldots, n_{i,j}, \]
# (e). (Type your code in the space below, if any)
length(unique(ChickWeight$Time))
## [1] 12
mod2 <- aov(weight ~ as.factor(Time) * Diet, data = ChickWeight)
anova(mod2)
## Analysis of Variance Table
##
## Response: weight
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(Time) 11 2067050 187914 157.8081 < 2.2e-16 ***
## Diet 3 129721 43240 36.3127 < 2.2e-16 ***
## as.factor(Time):Diet 33 86676 2627 2.2057 0.000172 ***
## Residuals 530 631110 1191
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# We can check for reduced model as well
mod2.red <- aov(weight ~ as.factor(Time) + Diet, data = ChickWeight)
anova(mod2, mod2.red)
## Analysis of Variance Table
##
## Model 1: weight ~ as.factor(Time) * Diet
## Model 2: weight ~ as.factor(Time) + Diet
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 530 631110
## 2 563 717785 -33 -86676 2.2057 0.000172 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(f) We want to take the chick-specific effect into account.
The new mixed effect model is based on the model in (e), where
Time is treated as a continuous covariate instead of a
categorical factor, and a random intercept and a random slope (of
Time) are added into the model. Report the fitted
coefficients of the fixed effects, and summarize your findings from this
model. Hint: You do not need to write down the new model, but you may
find it helpful.
Solution: (Type your answer here)
# (f). (Type your code in the space below, if any)
mod3 <- lmer(weight ~ Time * Diet + (1|Chick), data = ChickWeight)
summary(mod3)
## Linear mixed model fit by REML ['lmerMod']
## Formula: weight ~ Time * Diet + (1 | Chick)
## Data: ChickWeight
##
## REML criterion at convergence: 5466.9
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.3158 -0.5900 -0.0693 0.5361 3.6024
##
## Random effects:
## Groups Name Variance Std.Dev.
## Chick (Intercept) 545.7 23.36
## Residual 643.3 25.36
## Number of obs: 578, groups: Chick, 50
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 31.5143 6.1163 5.152
## Time 6.7115 0.2584 25.976
## Diet2 -2.8807 10.5479 -0.273
## Diet3 -13.2640 10.5479 -1.258
## Diet4 -0.4016 10.5565 -0.038
## Time:Diet2 1.8977 0.4284 4.430
## Time:Diet3 4.7114 0.4284 10.998
## Time:Diet4 2.9506 0.4340 6.799
##
## Correlation of Fixed Effects:
## (Intr) Time Diet2 Diet3 Diet4 Tm:Dt2 Tm:Dt3
## Time -0.426
## Diet2 -0.580 0.247
## Diet3 -0.580 0.247 0.336
## Diet4 -0.579 0.247 0.336 0.336
## Time:Diet2 0.257 -0.603 -0.431 -0.149 -0.149
## Time:Diet3 0.257 -0.603 -0.149 -0.431 -0.149 0.364
## Time:Diet4 0.254 -0.595 -0.147 -0.147 -0.432 0.359 0.359
mod3.other <- lmer(weight ~ Time * Diet + (1 + Time|Chick), data = ChickWeight)
summary(mod3.other)
## Linear mixed model fit by REML ['lmerMod']
## Formula: weight ~ Time * Diet + (1 + Time | Chick)
## Data: ChickWeight
##
## REML criterion at convergence: 4781.5
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.7223 -0.5672 -0.0343 0.4579 3.5184
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## Chick (Intercept) 116.91 10.812
## Time 10.92 3.305 -0.97
## Residual 163.37 12.782
## Number of obs: 578, groups: Chick, 50
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 33.6613 2.9192 11.531
## Time 6.2770 0.7614 8.245
## Diet2 -5.0277 5.0108 -1.003
## Diet3 -15.4110 5.0108 -3.076
## Diet4 -1.7505 5.0179 -0.349
## Time:Diet2 2.3321 1.3044 1.788
## Time:Diet3 5.1459 1.3044 3.945
## Time:Diet4 3.2550 1.3051 2.494
##
## Correlation of Fixed Effects:
## (Intr) Time Diet2 Diet3 Diet4 Tm:Dt2 Tm:Dt3
## Time -0.880
## Diet2 -0.583 0.513
## Diet3 -0.583 0.513 0.339
## Diet4 -0.582 0.512 0.339 0.339
## Time:Diet2 0.514 -0.584 -0.882 -0.299 -0.299
## Time:Diet3 0.514 -0.584 -0.299 -0.882 -0.299 0.341
## Time:Diet4 0.514 -0.583 -0.299 -0.299 -0.882 0.340 0.340
(g) Assume that the chicks in each diet are randomly selected from the same population, i.e., the enrollment of chicks is independent from any other factors. State the Stable Unit Treatment Value Assumption, write down the potential outcomes (weight at Day 20), and verify whether the randomization assumption holds. (This question will be replaced by another, since causal inference will not be covered this quarter.)
Solution: (Type your answer here)
LaTeX commands
\[Y_{i,j,k}, \mu, \alpha, \beta, \epsilon, i, j, H_0, H_a, \neq, =, \ldots\]